package com.ybl.store.backend.controller.deal;

import com.alibaba.excel.EasyExcel;
import com.ybl.store.backend.dto.req.deal.LogDealProductPaymentSaveReqDTO;
import com.ybl.store.backend.dto.req.deal.LogDealProductPaymentUpdateReqDTO;
import com.ybl.store.backend.dto.resp.deal.LogDealProductPaymentExportRespDTO;
import com.ybl.store.backend.dto.resp.deal.LogDealProductPaymentPageRespDTO;
import com.ybl.store.backend.service.deal.LogDealProductPaymentService;
import com.ybl.store.common.convention.errorcode.BaseErrorCode;
import com.ybl.store.common.convention.exception.ClientException;
import com.ybl.store.common.convention.result.Result;
import com.ybl.store.common.convention.result.Results;
import com.ybl.store.common.entity.ListRespDTO;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/backend/log/deal/product-payment")
public class LogDealProductPaymentController {
    
    private final LogDealProductPaymentService logDealProductPaymentService;

    @PostMapping
    public Result<Void> saveLogDealProductPayment(@RequestBody LogDealProductPaymentSaveReqDTO requestParam){
        logDealProductPaymentService.saveLogDealProductPayment(requestParam);
        return Results.success();
    }

    @DeleteMapping
    public Result<Void> deleteLogDealProductPayment(@RequestParam Long id) {
        logDealProductPaymentService.deleteLogDealProductPayment(id);
        return Results.success();
    }

    @PutMapping
    public Result<Void> updateLogDealProductPayment(@RequestBody LogDealProductPaymentUpdateReqDTO requestParam) {
        logDealProductPaymentService.updateLogDealProductPayment(requestParam);
        return Results.success();
    }

    @GetMapping("/page")
    public Result<ListRespDTO<LogDealProductPaymentPageRespDTO>> pageLogDealProductPayment(
            @RequestParam Integer pn,
            @RequestParam Integer ps,
            @RequestParam(required = false) Long dealProductId,
            @RequestParam(required = false) Long storeId,
            @RequestParam(required = false) Long clerkId,
            @RequestParam(required = false) Long customerId,
            @RequestParam(required = false) Long productId,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime)
    {
        return Results.success(logDealProductPaymentService.pageLogDealProductPayment(pn, ps, dealProductId, storeId, clerkId, customerId, productId, startTime, endTime));
    }

    @GetMapping("/export")
    public void export(HttpServletResponse response) {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=data.xlsx");

        try {
            List<LogDealProductPaymentExportRespDTO> export = logDealProductPaymentService.export();
            EasyExcel.write(response.getOutputStream(), LogDealProductPaymentExportRespDTO.class)
                    .sheet("data")
                    .doWrite(export);
        }
        catch (IOException e) {
            throw new ClientException(BaseErrorCode.EXCEL_ERROR);
        }
    }
}
